﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;

namespace INSTA.BankApplication.DAL
{
    public static class DbAccessorFactory
    {
        /// <summary>
        /// The key in the app.config file for the Database Type.
        /// </summary>
        public const string CONFIGKEY_DATABASETYPE      = "DatabaseType";

        /// <summary>
        /// The key in the app.config file for the Database connection string.
        /// </summary>
        public const string CONFIGKEY_CONNECTIONSTRING  = "ConnectionString";


        /// <summary>
        /// Creates the correct type of DbAccessor
        /// (SqlServer or MySql) depending on the 
        /// "DatabaseType" in the app.config file.
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <returns>The correct type of DbAccessor</returns>
        public static AbstractDbAccessor Create(DataMapper mapper)
        {
            AbstractDbAccessor result = null;

            // on récupère les valeurs des deux clés 
            // dans le fichier App.config..
            var databaseType        = ConfigurationManager.AppSettings[CONFIGKEY_DATABASETYPE];
            var connectionString    = ConfigurationManager.AppSettings[CONFIGKEY_CONNECTIONSTRING];

            if (databaseType == DatabaseTypeEnum.SqlServer.ToString())
            {
                result = new SqlServerDbAccessor(mapper, connectionString);    
            }
            else if (databaseType == DatabaseTypeEnum.MySql.ToString())
            {
                result = new MySqlDbAccessor(mapper, connectionString);    
            }
            else
            {
                throw new ConfigFileException("Unknown Database type : " + databaseType);
            }


            return result;
        }
    }
}
